<?php 
header("Content-type: text/html; charset=utf-8"); 
require_once('get_grade_curl.php');

include_once($_SERVER['DOCUMENT_ROOT']."/common/config.php");
$_conn = connect_mysql('wit_curriculum');

$get = new get_data_curl();

function build_json_data($data){
	$week = array(
		array("Sunday", "周日"),
		array("Monday", "周一"),
		array("Tuesday", "周二"),
		array("Wednesday", "周三"),
		array("Thursday", "周四"),
		array("Friday", "周五"),
		array("Saturday", "周六")
	);
	for($i=0; $i<=6; $i++){
		foreach($data as $row){
			if($row['weekday'] == $week[$i][0]){
				$list[$week[$i][0]][] = array(
					'title' => urlencode($row['title']),
					'startWeek' => $row['startWeek'],
					'stopWeek' => $row['stopWeek'],
					'time' => $row['time'],
					'switch' => $row['switch'],
					'teacher' => urlencode($row['teacher']),
					'area' => $row['area']
				);
			}
		}
	}
	return $list;
}

if(preg_match('/^\d{10}$/', $_GET['studentId'])){
	$studentId = $_GET['studentId'];
	$sql = "SELECT * FROM wit_curriculum WHERE uid = '$studentId'";
	$res = mysqli_query($_conn, $sql) or die;//(mysqli_error($_conn));
	$num = mysqli_num_rows($res);
	
	if(!$num){
		if(!$get->login('1203020301', '1203020301', '陈厚良')){
			if($get->get_error() == 'Failed to get random strings!'){
				$error = '教务处挂了_(:3」∠)_';
			}else{
				$error = '登陆失败！(1)';
			}
			//echo $get->get_error();
		}else{
			$pass = $get->get_student_pass($studentId);
			//print_r($pass);
			unset($get);
			$get = new get_data_curl();
			if(!$get->login($studentId, $pass[0], $pass[1])){
				if($get->get_error() == 'Failed to get random strings!'){
					$error = '教务处挂了_(:3」∠)_';
				}else{
					$error = '登陆失败！(2)';
				}
				//echo $get->get_error();
			}else{
				$list = $get->get_curriculum('2014-2015', '1');
				if($list){
					foreach($list as $row){
						$sql = "INSERT INTO wit_curriculum (uid, title, weekday, startWeek, stopWeek, time, switch, teacher, area) VALUES ('{$studentId}', '{$row['title']}', '{$row['weekday']}', '{$row['startWeek']}', '{$row['stopWeek']}', '{$row['time']}', '{$row['switch']}', '{$row['teacher']}', '{$row['area']}')";
						mysqli_query($_conn, $sql) or die;//(mysqli_error($_conn));
					}
				}else{
					$error = '获取失败';
				}
			}
		}
	}else{
		while($row = mysqli_fetch_assoc($res)){
			$list[] = array(
				'weekday' => $row['weekday'],
				'title' => $row['title'],
				'startWeek' => $row['startWeek'],
				'stopWeek' => $row['stopWeek'],
				'time' => $row['time'],
				'switch' => $row['switch'],
				'teacher' => $row['teacher'],
				'area' => $row['area']
			);
		}
	}	
}else{
	$error = '信息不全';
}

$list = build_json_data($list);
$json = array(
	'error' => isset($error) ? urlencode($error) : null,
	'curriculum' => $list
);
echo $json_string = urldecode(json_encode($json));
/*
$md5 = md5($json_string);
$sql = "INSERT INTO wit_md5 (uid, md5) SELECT '$studentId', '$md5' FROM dual WHERE NOT EXISTS (SELECT * FROM wit_md5 WHERE wit_md5.uid = '$studentId')";
mysqli_query($_conn, $sql) or die(mysqli_error($_conn));
*/
?>